package jobscheduling.gui;

import org.jfree.chart.*;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;

import jobscheduling.model.Problem;

/**
 *
 * @author Dawid
 */
public class GanttChart {
    public GanttChart (Problem problem, int[] permutations){
        TaskSeriesCollection dataset = new TaskSeriesCollection();
        
        int[][] rawJobs = problem.getRawJobs();
        
        TaskSeries[] taskSeriesTable = new TaskSeries[problem.getJobs().length];
        for (int i = 0; i < taskSeriesTable.length; ++i){
            taskSeriesTable[i] = new TaskSeries ("Job nr " + permutations[i]);
        }
        
        int machineNum = problem.getMachinesNumber();
        int jobNum = problem.getJobs().length;
        int[] jobStartTime = new int[jobNum];
        for (int i = 0; i < jobStartTime.length; ++i){
            jobStartTime[i] = 0;
        }
        for (int i = 0; i < machineNum; ++i){
            int start = 0;
            for (int j = 0; j < jobNum; ++j){
                if (jobStartTime[j] > start){
                    start = jobStartTime[j];
                }
                int stop = start + rawJobs[i][j];
                taskSeriesTable[j].add(
                    new Task("M"+i, new SimpleTimePeriod(start, stop))
                );
                start = jobStartTime[j] = stop;
            }
        }
        
        for (TaskSeries ts : taskSeriesTable){
            dataset.add(ts);
        }

        JFreeChart chart = ChartFactory.createGanttChart(problem.getName(), "Machine", "Time", dataset , true, false, false);


        ChartFrame frame1= new ChartFrame(problem.toString(),chart);
        frame1.setVisible(true);
        frame1.setSize(300,300);
    }
}
